package com.kehutong.admin.controler;

import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.jdbc.JdbcSession;

import com.kehutong.admin.entity.db.CorpDatabase;
import com.kehutong.common.util.Token;

@MvcAction
@ReqMapping("/admin/sys/corpdatabase")
public class CorpDatabaseController {

    @Inject
    private JdbcSession jdbcSession;

    @HasPermission("sys:cdb:view")
    @ReqMapping("/list")
    public Object list(JSONObject jsonObject) {
        return jdbcSession.findPage(CorpDatabase.class)
        		.like(jsonObject, "companyName")
				.eq("deleted", false)
				.page(jsonObject)
				.exe();
    }

    @HasPermission("sys:cdb:view")
    @ReqMapping("/get")
    public Object getById(CorpDatabase db) {
        return db;
    }

    @Bean(newInstance = true, copy = true)
    @HasPermission("sys:cdb:edit")
    @ReqMapping("/save")
    public Object save(Token token, CorpDatabase db) {
    	db.setUpdate_by(token.getUsername());
    	db.setCreate_by(token.getUsername());
    	jdbcSession.insert(db);
    	
        return db;
    }

    @Bean(copy = true)
    @HasPermission("sys:cdb:edit")
    @ReqMapping("/update")
    public Object update(Token token, CorpDatabase db) {
    	db.setUpdate_by(token.getUsername());
    	jdbcSession.updateById(db);
        return db;
    }

    @HasPermission("sys:cdb:edit")
    @ReqMapping("/delete")
    public Object deleteById(Token token, CorpDatabase db) {
    	
    	db.setDeleted(true);
    	db.setUpdate_by(token.getUsername());
    	jdbcSession.updateById(db);


        return Result.success();
    }
}
